using UnityEngine;
using UnityEngine.Networking;
using UnityEngine.UI;
using UnityEngine.Video;

/****************************************************
// 功能：加载界面
// | Talk To [ 聊来改善地区金融服务环境，助力企业发展 ]
// | Copyright (c) 2021-2025 聊来 All rights reserved.
// | Licensed ( <a href="http://www.apache.org/licenses/LICENSE-2.0">聊来</a> )
// | @Author: 贯彻己道的云 <18970881148@qq.com>
// | DateTime: 2025/1/3 8:58:16
*****************************************************/

public class LoadingWindow : BaseWindow
{
    private Image loadingFg;
    private Image imgPoint;
    private Text textProgress;
    private Button audioButton;
    public VideoPlayer videoPlayer;
    private RawImage rawImage;
    private Text tips;

    // 进度条实际宽度
    private float screenWidth;
    private int currentVideoPlayerTime;

    private int tipIndex;
    
    private UnityWebRequest web;

    private readonly string[] gameTips =
    {
        "正在为您准备加载游戏，请耐心等候。。。",
        "击杀敌方英雄有助于获得更多金币和经验",
        "合理利用召唤技能可以在战斗中扭转局势",
        "注意地图上的小兵刷新时间，合理控制经济优势"
    };

    private void Start()
    {
        loadingFg = FindChildTransform(transform, "loadingFg").GetComponent<Image>();
        textProgress = FindChildTransform(transform, "textProgress").GetComponent<Text>();
        audioButton = FindChildTransform(transform, "audioButton").GetComponent<Button>();
        rawImage = FindChildTransform(transform,"videoPlayer").GetComponent<RawImage>();
        tips = FindChildTransform(transform, "tips").GetComponent<Text>();
    }

    private void Update()
    {
        if (videoPlayer == null) return;
        currentVideoPlayerTime = (int) videoPlayer.time;
        PlayerPrefs.SetInt(Constants.OPEN_VIDEO_PLAYER, currentVideoPlayerTime);
        PlayerPrefs.Save();

    }

    protected override void InitWindows()
    {
        base.InitWindows();
        
        imgPoint = FindChildTransform(transform, "imgPoint").GetComponent<Image>();
        
        // Canvas Scaler来控制在各种分辨率保证UI的比例时使用Screen.width得到的宽度会和我们实际的宽度有差别
        screenWidth = 1080f / Screen.height * Screen.width;
        imgPoint.rectTransform.anchoredPosition = new Vector2(0, 0);
        //SetSprite(audioButton, "Images/Login/audio_close");
        
        InvokeRepeating(nameof(NextGameTip), 1.5f, 1.5f);

        resource.loadingWindow = this;

        web = UnityWebRequest.Get("http://images.pxphjr.cn/P1.mp4");
        StartCoroutine(SetDownViewPlayerToLocal(web, videoPlayer,"P1.mp4"));
    }
    
    
    protected override void ClearWindows()
    {
        base.ClearWindows();
        Destroy(videoPlayer);
        Destroy(rawImage);
        
        web?.Dispose();
    }
    
    

    /// <summary>
    /// 设置进度条显示
    /// </summary>
    /// <param name="progress"></param>
    public void SetProgress(float progress)
    {
        SetText(textProgress, (int)(progress * 100) + "%");
        loadingFg.fillAmount = progress;

        imgPoint.rectTransform.anchoredPosition = new Vector2(progress * (screenWidth - 200), 0);
    }

    /// <summary>
    /// 提示语
    /// </summary>
    private void NextGameTip()
    {
        string tip = gameTips[tipIndex];
        tipIndex++;
        if (tipIndex >= gameTips.Length)
        {
            tipIndex = 0;
        }
        SetText(tips, tip);
    }
}